// 弹出页面的JavaScript逻辑

document.addEventListener('DOMContentLoaded', function() {
  const toggleButton = document.getElementById('toggleCollect');
  const syncHistoryButton = document.getElementById('syncHistory');
  const syncBookmarksButton = document.getElementById('syncBookmarks');
  const uploadBookmarksButton = document.getElementById('uploadBookmarks');
  const exportHistoryButton = document.getElementById('exportHistory');
  const exportBookmarksButton = document.getElementById('exportBookmarks');
  const statusDiv = document.getElementById('status');

  // 检查数据收集状态
  chrome.storage.sync.get(['collectEnabled'], function(result) {
    const isEnabled = result.collectEnabled || false;
    updateStatus(isEnabled);
  });

  // 切换数据收集状态
  toggleButton.addEventListener('click', function() {
    chrome.storage.sync.get(['collectEnabled'], function(result) {
      const newStatus = !(result.collectEnabled || false);
      chrome.storage.sync.set({collectEnabled: newStatus}, function() {
        updateStatus(newStatus);
        // 显示状态变更提示
        const message = newStatus ? 
          "数据收集已启用，将开始收集浏览数据" : 
          "数据收集已禁用，将停止收集浏览数据";
        alert(message);
      });
    });
  });

  // 同步浏览历史
  syncHistoryButton.addEventListener('click', function() {
    // 禁用按钮并显示加载状态
    syncHistoryButton.disabled = true;
    syncHistoryButton.textContent = "同步中...";
    
    // 发送消息到background script触发浏览历史同步
    chrome.runtime.sendMessage({action: "syncHistory"}, function(response) {
      // 恢复按钮状态
      syncHistoryButton.disabled = false;
      syncHistoryButton.textContent = "同步浏览历史";
      
      if (chrome.runtime.lastError) {
        alert("同步浏览历史失败: " + chrome.runtime.lastError.message);
        return;
      }
      
      if (response && response.status === "success") {
        const message = response.message || "浏览历史同步成功！";
        alert(message);
      } else {
        const message = response.message || "浏览历史同步失败！";
        alert(message);
      }
    });
  });

  // 同步书签
  syncBookmarksButton.addEventListener('click', function() {
    // 禁用按钮并显示加载状态
    syncBookmarksButton.disabled = true;
    syncBookmarksButton.textContent = "同步中...";
    
    // 发送消息到background script触发书签同步
    chrome.runtime.sendMessage({action: "syncBookmarks"}, function(response) {
      // 恢复按钮状态
      syncBookmarksButton.disabled = false;
      syncBookmarksButton.textContent = "同步书签";
      
      if (chrome.runtime.lastError) {
        alert("同步书签失败: " + chrome.runtime.lastError.message);
        return;
      }
      
      if (response && response.status === "success") {
        const message = response.message || "书签同步成功！";
        alert(message);
      } else {
        const message = response.message || "书签同步失败！";
        alert(message);
      }
    });
  });

  // 上传书签到浏览器
  uploadBookmarksButton.addEventListener('click', function() {
    if (!confirm("确定要将服务器上的导航数据上传为浏览器书签吗？这将在您的书签中创建新的文件夹和书签。")) {
      return;
    }
    
    // 禁用按钮并显示加载状态
    uploadBookmarksButton.disabled = true;
    uploadBookmarksButton.textContent = "上传中...";
    
    // 发送消息到background script触发书签上传
    chrome.runtime.sendMessage({action: "uploadBookmarks"}, function(response) {
      // 恢复按钮状态
      uploadBookmarksButton.disabled = false;
      uploadBookmarksButton.textContent = "上传书签到浏览器";
      
      if (chrome.runtime.lastError) {
        alert("上传书签失败: " + chrome.runtime.lastError.message);
        return;
      }
      
      if (response && response.status === "success") {
        const message = response.message || "书签上传成功！";
        alert(message);
      } else {
        const message = response.message || "书签上传失败！";
        alert(message);
      }
    });
  });

  // 导出浏览历史
  exportHistoryButton.addEventListener('click', function() {
    // 发送消息到background script获取浏览历史数据
    chrome.runtime.sendMessage({action: "exportHistory"}, function(response) {
      if (chrome.runtime.lastError) {
        alert("导出浏览历史失败: " + chrome.runtime.lastError.message);
        return;
      }
      
      // 检查是否有消息提示
      if (response && response.message) {
        alert(response.message);
        return;
      }
      
      if (response && response.data) {
        // 检查是否有数据
        if (response.data.length === 0) {
          alert("尚未收集到浏览历史数据，请确保数据收集已启用并浏览一些网页后再尝试导出。");
          return;
        }
        
        // 创建并下载JSON文件
        const dataStr = JSON.stringify(response.data, null, 2);
        const dataBlob = new Blob([dataStr], {type: 'application/json'});
        const url = URL.createObjectURL(dataBlob);
        
        const link = document.createElement('a');
        link.href = url;
        link.download = 'browsing-history.json';
        link.click();
        
        URL.revokeObjectURL(url);
        alert("浏览历史导出成功！");
      } else {
        alert("导出浏览历史失败！可能尚未收集到浏览数据。");
      }
    });
  });

  // 导出书签
  exportBookmarksButton.addEventListener('click', function() {
    // 发送消息到background script获取书签数据
    chrome.runtime.sendMessage({action: "exportBookmarks"}, function(response) {
      if (chrome.runtime.lastError) {
        alert("导出书签失败: " + chrome.runtime.lastError.message);
        return;
      }
      
      if (response && response.data) {
        // 创建并下载JSON文件
        const dataStr = JSON.stringify(response.data, null, 2);
        const dataBlob = new Blob([dataStr], {type: 'application/json'});
        const url = URL.createObjectURL(dataBlob);
        
        const link = document.createElement('a');
        link.href = url;
        link.download = 'bookmarks.json';
        link.click();
        
        URL.revokeObjectURL(url);
        alert("书签导出成功！");
      } else {
        alert("导出书签失败！请检查扩展权限设置。");
      }
    });
  });

  // 更新状态显示
  function updateStatus(enabled) {
    if (enabled) {
      statusDiv.textContent = '数据收集已启用';
      statusDiv.className = 'status enabled';
      toggleButton.textContent = '禁用数据收集';
    } else {
      statusDiv.textContent = '数据收集已禁用';
      statusDiv.className = 'status disabled';
      toggleButton.textContent = '启用数据收集';
    }
  }
});